home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / EDITSEAR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.4 KB  |  225 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1992, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.7  $
  6. //
  7. // Implementation of class TEditWindow, an edit control that responds to Find,
  8. // Replace and FindNext commands.
  9. //----------------------------------------------------------------------------
  10. #pragma hdrignore SECTION
  11. #include <owl/pch.h>
  12. #if !defined(OWL_EDITSEAR_H)
  13. # include <owl/editsear.h>
  14. #endif
  15. #if !defined(OWL_APPLICAT_H)
  16. # include <owl/applicat.h>
  17. #endif
  18. #if !defined(OWL_EDIT_H)
  19. # include <owl/edit.h>
  20. #endif
  21. #if !defined(OWL_FINDREPL_H)
  22. # include <owl/findrepl.h>
  23. #endif
  24. #include <stdio.h>
  25.  
  26. OWL_DIAGINFO;
  27.  
  28. #if !defined(SECTION) || SECTION == 1
  29.  
  30. DEFINE_RESPONSE_TABLE1(TEditSearch, TEdit)
  31.   EV_COMMAND(CM_EDITFIND, CmEditFind),
  32.   EV_COMMAND_ENABLE(CM_EDITFIND, CeEditFindReplace),
  33.   EV_COMMAND(CM_EDITREPLACE, CmEditReplace),
  34.   EV_COMMAND_ENABLE(CM_EDITREPLACE, CeEditFindReplace),
  35.   EV_COMMAND(CM_EDITFINDNEXT, CmEditFindNext),
  36.   EV_COMMAND_ENABLE(CM_EDITFINDNEXT, CeEditFindNext),
  37.   EV_REGISTERED(FINDMSGSTRING, EvFindMsg),
  38. END_RESPONSE_TABLE;
  39.  
  40. //
  41. // Construct a TEditSearch window given some initial text.
  42. //
  43. TEditSearch::TEditSearch(TWindow*        parent,
  44.                          int             id,
  45.                          const char far* text,
  46.                          int x, int y, int w, int h,
  47.                          TModule*        module)
  48. :
  49.   TEdit(parent, id, text, x, y, w, h, 0, true, module),
  50.   SearchData(FR_DOWN)
  51. {
  52.   Attr.Style |= ES_NOHIDESEL;
  53.   SearchDialog = 0;
  54.   SearchCmd = 0;
  55. }
  56.  
  57. //
  58. //
  59. //
  60. TEditSearch::~TEditSearch()
  61. {
  62.   delete SearchDialog;
  63. }
  64.  
  65. //
  66. // Post a CM_EDITFIND or a CM_EDITREPLACE to re-open a previously open
  67. // find or replace modeless dialog
  68. //
  69. void
  70. TEditSearch::SetupWindow()
  71. {
  72.   TEdit::SetupWindow();
  73.   if (SearchCmd)
  74.     PostMessage(WM_COMMAND, SearchCmd);
  75. }
  76.  
  77. //
  78. // Perform a search or replace operation based on information in SearchData
  79. //
  80. void
  81. TEditSearch::DoSearch()
  82. {
  83.   do {
  84. #if defined(BI_PLAT_WIN32)  // && defined(WIN32S_SUPPORT) ??
  85.     if (TSystem::IsWin95() || TSystem::IsWin32s()) {  
  86.       if (GetApplication())
  87.         GetApplication()->PumpWaitingMessages();
  88.     }
  89. #endif
  90.     if (Search(-1, SearchData.FindWhat, bool(SearchData.Flags&FR_MATCHCASE),
  91.                bool(SearchData.Flags&FR_WHOLEWORD),
  92.                !(SearchData.Flags&FR_DOWN)) >= 0) {
  93.       if (SearchData.Flags & (FR_REPLACE|FR_REPLACEALL))
  94.         Insert(SearchData.ReplaceWith);
  95.     }
  96.     else {
  97.       if (SearchData.Flags & (FR_FINDNEXT|FR_REPLACE)) {
  98.         string errTemplate(GetModule()->LoadString(IDS_CANNOTFIND));
  99.         char  errMsg[81];
  100.         sprintf(errMsg, errTemplate.c_str(), (const char far*)SearchData.FindWhat);
  101.         GetApplication()->MessageBox(0, errMsg, GetApplication()->GetName(),
  102.                       MB_OK | MB_ICONEXCLAMATION | MB_TASKMODAL);
  103.       }
  104.       else if (SearchData.Flags & FR_REPLACEALL)
  105.         break;
  106.     }
  107.   } while (SearchData.Flags & FR_REPLACEALL);
  108. }
  109.  
  110. //
  111. // Open the modeless Find commdlg
  112. //
  113. void
  114. TEditSearch::CmEditFind()
  115. {
  116.   if (!SearchCmd) {
  117.     SearchCmd = CM_EDITFIND;
  118.     delete SearchDialog;
  119.     SearchDialog = new TFindDialog(this, SearchData);
  120.     SearchDialog->Create();
  121.   }
  122. }
  123.  
  124. //
  125. // Open the modeless Replace commdlg
  126. //
  127. void
  128. TEditSearch::CmEditReplace()
  129. {
  130.   if (!SearchCmd) {
  131.     SearchCmd = CM_EDITREPLACE;
  132.     delete SearchDialog;
  133.     SearchDialog = new TReplaceDialog(this, SearchData);
  134.     SearchDialog->Create();
  135.   }
  136. }
  137.  
  138. //
  139. // Enable the find or replace option only if no dialog is up
  140. //
  141. void
  142. TEditSearch::CeEditFindReplace(TCommandEnabler& ce)
  143. {
  144.   ce.Enable(!SearchCmd);
  145. }
  146.  
  147. //
  148. // Respond to the possible separate menu command to repeat the search
  149. //
  150. void
  151. TEditSearch::CmEditFindNext()
  152. {
  153.   if (SearchDialog)
  154.     SearchDialog->UpdateData();
  155.   SearchData.Flags |= FR_FINDNEXT;
  156.   DoSearch();
  157. }
  158.  
  159. //
  160. // Only enable FindNext if we've got data to search for
  161. //
  162. void
  163. TEditSearch::CeEditFindNext(TCommandEnabler& ce)
  164. {
  165.   ce.Enable(SearchData.FindWhat && *SearchData.FindWhat);
  166. }
  167.  
  168. //
  169. // Respond to the message sent by the modeless find/replace dialog by
  170. // performing a search. Or, if the dialog has terminated, zero search command
  171. //
  172. TResult
  173. TEditSearch::EvFindMsg(TParam1, TParam2 param2)
  174. {
  175.   PRECONDITION(SearchDialog);
  176.  
  177.   SearchDialog->UpdateData(param2);
  178.   if (SearchData.Flags & FR_DIALOGTERM)
  179.     SearchCmd = 0;
  180.  
  181.   else
  182.     DoSearch();
  183.   return 0;
  184. }
  185.  
  186.  
  187. #endif
  188. //----------------------------------------------------------------------------
  189. #if !defined(SECTION) || SECTION == 2
  190.  
  191. IMPLEMENT_STREAMABLE1(TEditSearch, TEdit);
  192.  
  193. #if !defined(BI_NO_OBJ_STREAMING)
  194.  
  195. //
  196. // reads an instance of TEditSearch from the passed ipstream.
  197. // Re-opens the modeless find or replace dialog if one was up.
  198. //
  199. void*
  200. TEditSearch::Streamer::Read(ipstream& is, uint32 /*version*/) const
  201. {
  202.   ReadBaseObject((TEdit*)GetObject(), is);
  203.  
  204.   GetObject()->SearchData.Read(is);
  205.   is >> GetObject()->SearchCmd;
  206.   GetObject()->SearchDialog = 0;
  207.   return GetObject();
  208. }
  209.  
  210. //
  211. // writes the TEditSearch to the passed opstream
  212. //
  213. void
  214. TEditSearch::Streamer::Write(opstream& os) const
  215. {
  216.   WriteBaseObject((TEdit*)GetObject(), os);
  217.  
  218.   GetObject()->SearchData.Write(os);
  219.   os << GetObject()->SearchCmd;
  220. }
  221.  
  222. #endif  // if !defined(BI_NO_OBJ_STREAMING)
  223.  
  224. #endif
  225.